import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:top_foundation/component/calendar/widgets/year.dart';
import 'package:top_foundation/top_foundation.dart';

import 'index.dart';
import 'widgets/widgets.dart';

class CalendarView extends StatelessWidget {
  CalendarView({
    Key? key,
    bool isRange = false,
    required CalendarResult result,
  }) : super(key: key) {
    state.isRange = isRange;
    state.result = result;
  }
  final controller = Get.put(CalendarViewController());
  final state = Get.find<CalendarViewController>().state;
  // 主视图
  Widget _buildView() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        Expanded(
          child: GestureDetector(
            onTap: controller.back,
            behavior: HitTestBehavior.opaque,
          ),
        ),
        Container(
          color: Colors.white,
          child: SafeArea(
            top: false,
            child: Column(
              children: [
                Container(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      TextButton(
                          onPressed: controller.back,
                          child: Text(
                            '取消',
                            style: TextStyle(
                              color: const Color(0xff666666),
                              fontSize: 15.sp,
                            ),
                          )),
                      TextButton(
                          onPressed: controller.actionSure,
                          child: Text(
                            '确定',
                            style: TextStyle(
                              color: const Color(0xff333333),
                              fontSize: 15.sp,
                            ),
                          )),
                    ],
                  ),
                ),
                Container(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      IconButton(
                        onPressed: controller.actionLastMonth,
                        icon: const Icon(
                          Icons.arrow_back_ios_outlined,
                          color: Color(0xff333333),
                        ),
                        iconSize: 20.r,
                      ),
                      Obx(
                        () => Row(
                          children: [
                            InkWell(
                              onTap: controller.actionShowYear,
                              child: Row(
                                children: [
                                  Text(
                                    '${state.curr.value.year}年',
                                    style: TextStyle(
                                      fontSize: 13.r,
                                    ),
                                  ),
                                  Icon(
                                    Icons.arrow_drop_down,
                                    color: const Color(0xff333333),
                                    size: 22.r,
                                  )
                                ],
                              ),
                            ),
                            SizedBox(
                              width: 10.r,
                            ),
                            Text(
                              '${state.curr.value.month.month}月',
                              style: TextStyle(
                                fontSize: 15.sp,
                              ),
                            ),
                          ],
                        ),
                      ),
                      IconButton(
                        onPressed: controller.actionNextMonth,
                        icon: const Icon(
                          Icons.arrow_forward_ios_rounded,
                          color: Color(0xff333333),
                        ),
                        iconSize: 20.r,
                      ),
                    ],
                  ),
                ),
                state.isShowYearView ? CalendarYearView() : const MonthView(),
              ],
            ),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CalendarViewController>(
      builder: (_) {
        return Scaffold(
          backgroundColor: state.bgColor,
          body: _buildView(),
        );
      },
    );
  }
}
